package educar.models;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import educar.db.DbConnection;
import educar.db.JPA;

public class Solucion {
	
	private static JPA jpa = new JPA();
	private String fecha;
	private Integer id_act;
	private String dni;
	
	public Solucion(String fecha, Integer id_act, String dni){
		this.setFecha(fecha);
		this.setIdAct(id_act);
		this.setDni(dni);
	}
	
	public boolean save() {
		try {
			Solucion.getSolucionBySolucion(Integer.toString(id_act));
			return false;
		} catch (datoNotFound e1) {
			String[] columns = {"fecha","id_act","dni"};

			PreparedStatement stm = jpa.newRecord("educar_dev.solucion", columns);

			try {
				stm.setString(1,fecha);
				stm.setString(2,Integer.toString(id_act));
				stm.setString(3,dni);
			} catch (SQLException e) {
				e.printStackTrace();
				return false;
			}

			jpa.create(stm);
			return true;
		}
	}
	
	public static Solucion getSolucionBySolucion(String id_act) throws datoNotFound {
		ResultSet rs = null;
		Solucion p = null;

		rs = jpa.getByField("solucion", "id_act", id_act);
		
		try {
			if (rs.next()) {
				p = new Solucion(rs.getString(1),Integer.parseInt(rs.getString(2)),rs.getString(3));
			} else {
				throw new datoNotFound();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return p;
	}

	public void destroy(String fecha,Integer id_act,String dni) {
		String query="DELETE from " + DbConnection.bd + ".solucion"; 
    	query += " Where dni= '" + dni + "' and id_act= '"+id_act+"' and fecha= '"+fecha+"'";
		jpa.runUpdate(query);
	}
	
	public void setFecha(String fecha){
		this.fecha=fecha;
	}
	
	
	public String getFecha(){
		return this.fecha.toString();
	}
	
	public String getDni(){
		return this.dni;
	}
	
	public void setDni (String dni){
		this.dni=dni;
	}
	
	public Integer getIdAct(){
		return this.id_act;
	}
	
	public void setIdAct(Integer id_act){
		this.id_act=id_act;
	}
}
